package zz.demo.springCloud.service;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import zz.demo.springCloud.config.AmqpConfig;

import java.util.UUID;

@Service
public class SecKillService {
    private final static String NAME = "sec";
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;


    public String doSecKill(String username) {
        if (redisTemplate.opsForValue().increment(NAME, -1) >= 0) {
            rabbitTemplate.convertAndSend("topic","topic.123",username);
            return "OK: " + username;
        }
        return "error";
    }

    public String doRefresh() {
        redisTemplate.opsForValue().set(NAME, "100");
        return "OK";
    }


}
